package com.earnings.okhttputils.utils.ui.activity.common;

import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.earnings.R;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;



/**
 * Created by Administrator on 2016/10/24.
 * 网络请求页面
 */

public class WebActivity extends GodLeftHandBaseActivity {

    private WebView webView;
    private ProgressBar myCardProgressBar;
    private String url = "http://www.zcb365.com/user/vip.html";



    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void init() {
        setTitle(bundleData.getString("title"));
        myCardProgressBar = (ProgressBar) findViewById(R.id.myCardProgressBar);
        webView = (WebView) findViewById(R.id.newsDetails_webView_id);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        webView.setWebChromeClient( new MyWebChromeClient());
        webView.requestFocusFromTouch();
        webView.loadUrl(bundleData.getString("url"));
    }


    private class  MyWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                myCardProgressBar.setVisibility(View.GONE);
            } else {
                if (View.INVISIBLE == myCardProgressBar.getVisibility()) {
                    myCardProgressBar.setVisibility(View.VISIBLE);
                }
                myCardProgressBar.setProgress(newProgress);
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        }
        else {
            this.finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(webView != null){
        }
        webView.destroy();
    }

}
